/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.VaiTroGroup_DTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class VaiTroGroup_DAO {
    
    public static ArrayList<VaiTroGroup_DTO> GetALL()
    {
        ArrayList<VaiTroGroup_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from VaiTroGroup" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                VaiTroGroup_DTO VaiTro = new VaiTroGroup_DTO();
                VaiTro.setID(rs.getInt("ID"));
                VaiTro.setMaVaiTro(rs.getString("MaVaiTro"));
                VaiTro.setGroupID(rs.getString("GroupID"));
                VaiTro.setDienGiai(rs.getString("DienGiai"));
                VaiTro.setActive(rs.getBoolean("Active"));
                ds.add(VaiTro);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(VaiTroGroup_DTO VaiTro)
    {
        boolean flag = false;
        try {
            String sql = "Insert VaiTroGroup(MaVaiTro, GroupID, DienGiai, Active) Values('"+VaiTro.getMaVaiTro()+"','"+VaiTro.getGroupID()+"','"+VaiTro.getDienGiai()+"','"+VaiTro.isActive()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(VaiTroGroup_DTO VaiTro)
    {
        boolean flag = false;
        try {
            String sql = "Update VaiTroGroup Set MaVaiTro = '"+ VaiTro.getMaVaiTro() +"',GroupID = '"+VaiTro.getGroupID()+"', DienGiai = '"+VaiTro.getDienGiai()+"', Active = "+VaiTro.isActive()+" where ID = "+ VaiTro.getID();
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(int maVT)
    {
        boolean flag = false;
        try {
            String sql = "Delete from VaiTroGroup where ID = "+ maVT;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
